import darkdetect
from PySide6.QtCore import QObject, Signal ,QTimer

from utils import cfg_util

class DarkModeDetector(QObject):
    dark_changed = Signal(bool)

    def __init__(self, parent: QObject) -> None:
        super().__init__(parent=parent, objectName='DarkModeDetector')

        self._last_value = darkdetect.isDark()

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.checkDarkMode)
        self.timer.start(1 // cfg_util.config_instance.fps)

    def checkDarkMode(self):
        dark = darkdetect.isDark()
        if dark != self._last_value:
            self.dark_changed.emit(dark)
            self._last_value = dark